//ST表-模版题(RMQ问题)
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int f[N][25]; 
int lg[N];
int n, m; 

void init()
{
    lg[0] = -1;
    for(int i = 1; i <= n; i++)
    {
        lg[i] = lg[i >> 1] + 1;
        f[i][0] = a[i];
    }

    for(int j = 1; j <= lg[n]; j++) 
        for(int i = 1; i + (1 << j) - 1 <= n; i++)
            f[i][j] = max(f[i][j-1], f[i + (1 << (j - 1))][j-1]);
}

int query(int l, int r)
{
    int k = lg[r - l + 1];
    return max(f[l][k], f[r - (1 << k) + 1][k]);
}

int main()
{
    // cin >> n >> m;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++) 
    {
        // cin >> a[i];
        scanf("%d", &a[i]);
    }
    init();
    for(int i = 1; i <= m; i++)
    {
        int l, r; 
        // cin >> l >> r;
        scanf("%d%d", &l, &r);
        // cout << query(l, r) << endl;
        printf("%d\n", query(l, r));
    }
    return 0;
}